<template>
  <view>
    <Navbar :hideBtn="false" bgColor="#f3f4f6"></Navbar>
    <view style="background-color: #2b85e4; padding: 40rpx;">
      <view style="width: 140rpx; height: 140rpx; border: 1px solid #fff; border-radius: 50%; margin: 0 auto;">
        <u-avatar src="/static/img/avatar.png" size="120rpx" style="margin: 10rpx;"></u-avatar>
      </view>
    </view>
    <view style="padding: 40rpx;">
		<uni-forms ref="form" :model="formData" :rules="rules" labelWidth="80px">
			<uni-forms-item label="用户昵称" required name="nickName">
				<uni-easyinput v-model="formData.nickName" placeholder="请输入用户昵称" />
			</uni-forms-item>
			<uni-forms-item label="手机号码" required name="phonenumber">
				<uni-easyinput v-model="formData.phonenumber" placeholder="请输入手机号码" />
			</uni-forms-item>
			
			<uni-forms-item label="邮箱" required name="email">
				<uni-easyinput v-model="formData.email" placeholder="请输入邮箱" />
			</uni-forms-item>
			
			<uni-forms-item label="性别" required name="sex">
				<uni-data-checkbox v-model="formData.sex" :localdata="sexs" />
			</uni-forms-item>
		</uni-forms>
		<u-row gutter="32">
		  <u-col span="6">
		    <u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
		  </u-col>
		  <u-col span="6">
				      <u-button icon="checkmark-circle" text="保存" type="primary" @click="submit"></u-button>
		  </u-col>
		</u-row>
    </view>
	<buttom-tabbar></buttom-tabbar>
  </view>
</template>

<script>
import Navbar from '@/components/navbar/Navbar'
import * as UserInfoApi from '@/api/user'
import * as DictInfoApi from '@/api/system/dict/data'

export default {
  components: {
    Navbar,
  },
  data () {
    return {
		formData:{ 
			nickName: '',
			sex: '',
			email: '',
			phonenumber: '',
		},
	  rules: {
	  	nickName: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '用户昵称不能为空'
	  		}]
	  	},
	  	phonenumber: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '手机号码不能为空'
	  		}]
	  	},
	  	email: {
	  			rules: [{
	  				required: true,
	  				errorMessage: '邮箱不能为空'
	  			}]
	  		}
	  },
	  sexs:[]
    }
  },
  created () {
	  DictInfoApi.getDicts("sys_user_sex").then(res => {
	  	if(res.data)
	  	{
	  		if(res.data.length)
	  		{
	  			this.sexs=[]
	  			for(let i=0;i<res.data.length;i++)
	  			{
	  				let temp = res.data[i];
	  				let obj = {};
	  				obj.value = temp.dictValue;
	  				obj.text = temp.dictLabel
	  				this.sexs.push(obj);
	  			}
	  		}
	  	}
	  });
	  let user = JSON.stringify(this.$store.getters.user);
	  this.formData = JSON.parse(user);
  },
  methods: {
	  submit() {
		this.$refs.form.validate().then(res => {
		    UserInfoApi.updateUserProfile(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
			  this.$store.commit('SET_USER', this.formData);
			  this.goBack()
		    })
		});
    },
    goBack () {
      uni.navigateBack({ delta: 1});
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
